package cn.daomeng.firebird.core.utils;

import com.alibaba.fastjson.JSON;

import cn.daomeng.firebird.core.exception.BizCode;
import cn.daomeng.firebird.core.exception.BizCodeEnumDefault;
import cn.daomeng.firebird.core.exception.FirebirdException;

import org.apache.commons.lang3.StringUtils;

/**
 * @author wf.shu
 * @version 1.0.0.0
 * @since 2019-03-08 15:09
 */
public class LogUtils {
    /**
     * 填充log字符串的占位符，并返回拼装结果
     *
     * @author  wf.shu
     * @since   ${DATE} ${TIME}
     * @version 1.0.0.0
     * @param message 带{}占位符的字符串
     * @param objects 参数值
     * @return
     */
    public static String format(String message ,Object ... objects){
        if(null==objects || objects.length==0){
            return message;
        }
        if(StringUtils.isEmpty(message) ){
            throw new FirebirdException(BizCode.getBizCodeFromEnum(BizCodeEnumDefault.BAD_REQUEST), "参数为空 : NaN");
        }
        String[] msgSplit = message.split("\\{\\}");
        if(msgSplit.length!=objects.length){
            throw new FirebirdException(BizCode.getBizCodeFromEnum(BizCodeEnumDefault.BAD_REQUEST), "参数个数与值个数不匹配");
        }
        StringBuilder stringBuilder  = new StringBuilder();
        for (int i=0;i<msgSplit.length;i++){
            stringBuilder.append(msgSplit[i])
                    .append(JSON.toJSONString(objects[i]));
        }
        return stringBuilder.toString();
    }
}
